-- 玩家数据模块
-- Author: Colocasia
-- Date: 2018-07-12

local class = require 'class';
local table = require 'us.table';
local log = require 'log';
local character = require 'character';

---常用函数引用
---@type fun table.merge @合并函数
local table_merge = table.merge;
local table_insert = table.insert;

---@class player_data;
local M = class('player_data');


---构造函数
---@param guid string
function M:ctor(guid)
    self:init_player_data(guid);
end

---初始化玩家
---@param guid string | 'guid'
function M:init_player_data(guid)
    self.server     = '';           -- 玩家服务器
    self.agent      = '';           -- 玩家代理地址
    self.name       = '';           -- 玩家名
    self.guid       = guid or '';   -- 玩家guid
    self.level      = 1;            -- 玩家等级
    self.exp        = 0;            -- 玩家经验
    self.head       = '';           -- 玩家头像
    self.char_list  = {};           -- 玩家角色
    self.char_obj   = {};           -- 角色对象
end

---加载玩家数据
---@param guid string
---@return table
function M:launch(guid)
    local data = self:find_player_by_guid(guid);
    if not data then return end;
    table_merge({self, data});

    -- 计算等级
    self:calc_level();

    -- mark 测试时期的代码，角色初始+1 还是靠抽还没想好
    if 1 == self.level and #self.char_list < 1 then
        table_insert(self.char_list, self:add_character(1));
    end

    --加载character
    for _, char_guid in pairs(self.char_list) do
        table_insert(self.char_obj, character.create(char_guid));
    end

    log.dump(self.char_obj);
end

---计算玩家等级
---@return player_data
function M:calc_level()
    self.level = 0 == self.exp and 1 or 1;
end

return M;
